from bcat.api.impl.c.capi import CApi, CApiInstance
import os
from enum import Enum 


from bcat.api.log import Log
class LogImpl(object):
    class Level(Log.Level):
        def __init__(self, v):
            super().__init__(v)
        @staticmethod
        def debug():
            return Log.Level(CApi.LOG_LEVEL_DEBUG)
        @staticmethod
        def trace():
            return Log.Level(CApi.LOG_LEVEL_TRACE)
        @staticmethod
        def notice():
            return Log.Level(CApi.LOG_LEVEL_NOTICE)
        @staticmethod
        def warning():
            return Log.Level(CApi.LOG_LEVEL_WARNING)
        @staticmethod
        def error():
            return Log.Level(CApi.LOG_LEVEL_ERROR)
        @staticmethod
        def fatal():
            return Log.Level(CApi.LOG_LEVEL_FATAL)

        def __str__(self):
            var = { 
                CApi.LOG_LEVEL_DEBUG    :"level.debug",
                CApi.LOG_LEVEL_TRACE    :"level.trace",
                CApi.LOG_LEVEL_NOTICE   :"level.notice",
                CApi.LOG_LEVEL_WARNING  :"level.warning",
                CApi.LOG_LEVEL_ERROR    :"level.error",
                CApi.LOG_LEVEL_FATAL    :"level.fatal",
            }
            if self.value() in var:
                return var[self.value()]
            else:
                return "unknow level"


    class Builder(Log.Builder):
        def __init__(self):
            super(LogImpl.Builder, self).__init__(".", "file", LogImpl.Level.trace(), False, 100)
            self.__api = CApiInstance.get()

        def api(self, api):
            if api is not None:
                self.__api = api
            return self

        def make(self): 
            log = LogImpl(self.__api, self.__dir_name, self.__file_name, self.__level)

            if self.__enable_date_roll:
                log.enable_date_rolling()
            else:
                log.disable_date_rolling()

            log.set_file_rolling_size(self.__file_size_roll)

            return log

        def __str__(self):
            return "api:[{}],dir:[{}],file:[{}],level:[{}],date:[{}],size:[{}]M".format(id(self.__api), self.__dir_name, self.__file_name, self.__level, self.__enable_date_roll, self.__file_size_roll)


    #private use
    def __init__(self, api, dir_name, file_name, level):
        #no check
        self.__api = api
        self.__dir_name = dir_name
        self.__file_name = file_name
        self.__level = level
        self.__impl = self.__api.log_create(self.__dir_name, self.__file_name, self.__level.value())

        self.__enable_date_roll = False
        self.__file_size_roll = None
        

    def dir_name(self):
        return self.__dir_name
    def file_name(self):
        return self.__file_name
    def level(self, level):
        return self.__level
        
    def date_roll_enabled(self):
        return self.__enable_date_roll
    def roll_size(self):
        return self.__file_size_roll

    def path(self):
        return self.__api.log_get_path(self.__impl)


    
    def set_level(self, level):
        self.__level = level
        self.__api.log_set_level(self.__impl, level.value())
        return self

    def enable_date_rolling(self):
        if not self.__enable_date_roll:
            self.__enable_date_roll = True
            self.__api.log_enable_date_roll(self.__impl)
        return self

    def disable_date_rolling(self):
        if self.__enable_date_roll:
            self.__enable_date_roll = False
            self.__api.log_disable_date_roll(self.__impl)
        return self

    def set_file_rolling_size(self, nsize):
        if self.__file_size_roll != nsize:
            self.__file_size_roll = nsize
            self.__api.log_enable_size_roll(self.__impl, self.__file_size_roll)
        return self

    def destroy(self):
        if self.__impl is not None:
            self.__api.log_destroy(self.__impl)
            self.__impl = None

    def __str__(self):
        return "Log-c-impl:[{}],impl[{}],dir:[{}],file:[{}],level:[{}],date:[{}],size:[{}]M".format(id(self.__api), id(self.__impl), self.__dir_name, self.__file_name, self.__level, self.__enable_date_roll, self.__file_size_roll)

